package com.grwx.omc.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grwx.omc.admin.entity.SysDevice;
import com.grwx.omc.admin.entity.SysProvider;
import com.grwx.omc.admin.service.ISysDeviceService;
import com.grwx.omc.admin.service.ISysProviderService;
import com.grwx.omc.constant.Constant;
import com.grwx.omc.entity.ResponseData;
import com.grwx.omc.exception.ValidationException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @Description: 厂商管理
 * @Author: ManJiang
 * @Date: 2020/5/14
 */
@RestController
@RequestMapping(value = "/sys_provider")
public class SysProviderController {

    @Autowired
    private ISysProviderService providerService;


    @ApiOperation("厂商查询分页")
    @PostMapping(value = "/search/{page}/{size}")
    public ResponseData page(@RequestBody SysProvider sysProvider, @PathVariable("page") int page, @PathVariable("size") int size){
        Page<SysProvider> sysProviderPage = new Page<>(page,size);

        IPage<SysProvider> result = providerService.page(sysProviderPage,sysProvider);

        return ResponseData.success(result);
    }

    @ApiOperation("厂商新增")
    @PostMapping()
    public ResponseData insert(@RequestBody @Valid SysProvider sysProvider){
        if (null == sysProvider) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }

        providerService.addOrUpdate(sysProvider);
        return ResponseData.success(Constant.MsgCode.INSERT_SUCCESS);
    }

    @ApiOperation("厂商修改")
    @PutMapping()
    public ResponseData update(@RequestBody @Valid SysProvider sysProvider){
        if (null == sysProvider) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }

        providerService.addOrUpdate(sysProvider);
        return ResponseData.success(Constant.MsgCode.UPDATE_SUCCESS);
    }

    @ApiOperation("厂商删除")
    @DeleteMapping()
    public ResponseData delete(Long[] ids){
        if (null==ids || ids.length==0){
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }

        providerService.del(ids);
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }
}
